<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\annotations;

class AnnotationDefinition
{
	/**
	 * @var string A name of the annotation.
	 */
	public $name;
	
	/**
	 * @var string? A name of the parameter this annotation belongs to.
	 */
	public $parameter;
	
	/**
	 * @var string A name of the annotation class.
	 */
	public $type;
	
	/**
	 * @var array<mixed> An array of constructor arguments.
	 */
	public $arguments = array();
	
	/**
	 * @var array<string, mixed> An array of properties.
	 */
	public $properties = array();
	
	/**
	 * Constructs a new annotation definition.
	 * 
	 * @param  string $name
	 *         A name of the annotation.
	 * @param  string? $type
	 *         An optional type of the annotation.
	 * @param  string? $parameter
	 *         An optional name of the routine parameter.
	 * @param  array<mixed>?
	 *         An array of annotation constructor arguments.
	 * @param  array<string, mixed>? $properties
	 *         An array of annotation properties.
	 * @throws InvalidArgumentException
	 *         If name is empty.
	 */
	public function __construct(
		$name,
		$type = null,
		$parameter = null,
		array $arguments = array(),
		array $properties = array())
	{
		if (empty($name))
		{
			throw new InvalidArgumentException('Name cannot be empty.');
		}
		
		$this->name       = $name;
		$this->type       = $type;
		$this->parameter  = $parameter;
		$this->arguments  = $arguments;
		$this->properties = $properties;
	}
	
	/**
	 * @return string
	 */
	public function __toString()
	{
		$str = '@' . $this->name;
		
		if (!empty($this->type))
		{
			$str .= '<' . $this->type . '>';
		}
		
		if (!empty($this->parameter))
		{
			$str .= '[' . $this->parameter . ']';
		}
		
		$params = '';
	
		foreach ($this->arguments as $v)
		{
			$params .= ', ' . (is_object($v) ? get_class($v) : gettype($v));
		}
		
		foreach ($this->properties as $k => $v)
		{
			$params .= ', ' . $k . '=' .
				(is_object($v) ? get_class($v) : gettype($v));
		}
		
		return $str . '(' . substr($params, 2) . ')';
	}
}